// VBTableMonth.cpp
// www.vbmcgi.org

#include "VBMcgi/VBMcgi.h"

using namespace br::com::sbVB::VBLib;
using namespace br::com::sbVB::VBMcgi;
using namespace std;


int br::com::sbVB::VBMcgi::VBTableMonth::getWeekMax() 
{
	int nDays = getMonthDayMax();
	int weekDayofDay1 = getWeekday(1);
	int ret = 5; // correct for most months
	if ((weekDayofDay1==VBsun) && (nDays==28))
		ret = 4;
	if ( ((weekDayofDay1==VBfri) && (nDays == 31)) || 
		 ((weekDayofDay1==VBsat) && (nDays >= 30)) )
		ret = 6;
	return ret;
} 

br::com::sbVB::VBMcgi::VBTableMonth::VBTableMonth() 
{
	setBorder(1);

	setWeekDays(
		"Sunday",
		"Monday",
		"Tuesday",
		"Wednsday",
		"Thursday",
		"Friday",
		"Saturday"
	);

	setSundayColor("#ffffbb");
	setMondayColor("#ffffff");
	setTuesdayColor("#ffffff");
	setWednsdayColor("#ffffff");
	setThursdayColor("#ffffff");
	setFridayColor("#ffffff");
	setSaturdayColor("#ffffcc");
	setNoDayColor("#ddcccc");
	setValign("top");
	setWidth(80);
	setHeight(80);
}

void br::com::sbVB::VBMcgi::VBTableMonth::setWeekDays(
	const char *Sunday,
	const char *Monday,
	const char *Tuesday,
	const char *Wednsday,
	const char *Thursday,
	const char *Friday,
	const char *Saturday) 
{
	m_weekDay[0] = Sunday;
	m_weekDay[1] = Monday;
	m_weekDay[2] = Tuesday;
	m_weekDay[3] = Wednsday;
	m_weekDay[4] = Thursday;
	m_weekDay[5] = Friday;
	m_weekDay[6] = Saturday;
}

void br::com::sbVB::VBMcgi::VBTableMonth::setDayContent(int day, VBString content) 
{
	day--; // to make the day array m_dayContent start from zero
	if (day >=0 && day <=31) // prevent access outside the array
		m_dayContent[day] = content;
}

void br::com::sbVB::VBMcgi::VBTableMonth::setDayColor(int day, VBString color)
{
	day--; // to make the day array m_dayContent start from zero
	if (day >=0 && day <=31) // prevent access outside the array
		m_dayColor[day] = color;
}



void br::com::sbVB::VBMcgi::VBTableMonth::htmlOut(std::ostream & out) 
{
	cout << "<table border=\"" << m_border << "\">" << endl;
	cout << "<tr valign=\"" << m_valign << "\">" << endl;
	int i;
	// title of the table
	for (i=0 ; i < 7 ; i++) // 7 days in a week
		cout << "<td>" << m_weekDay[i] << "</td>" << endl;
	cout << "</tr>" << endl;
	int weekMax = getWeekMax();
	int weekDay = getWeekday(1); // weekDay of day 1 of this month
	int monthDayMax = getMonthDayMax();
	int monthDay = 0;
	VBString content;
	VBString theColor;
	// content of the table
	for (int week=0 ; week < weekMax ; week++) 
	{
		cout << "<tr";
		if (!m_valign.IsEmpty())
			cout << " valign=\"" << m_valign << "\"";
		cout << ">" << endl;
		for (i=0 ; i < 7 ; i++) // 7 days in a week
		{ 
			theColor = m_color[i];
			bool leadEmpty = (week == 0) && (i < weekDay);
			if ( !leadEmpty && (monthDay <= monthDayMax))  monthDay++;
			content = "";
			content += monthDay;

			// if there's a special color for the day pointed to monthDay, overwrite the background color
			// for next table data
			if (!m_dayColor[monthDay-1].IsEmpty())
				theColor = m_dayColor[monthDay-1];

			if ( leadEmpty || (monthDay > monthDayMax) ) 
			{ // if day does not exist
				content = "&nbsp;";
				theColor = m_noDayColor;
			}
			else 
			{ // if day does exist
				if (!m_dayContent[monthDay-1].IsEmpty()) 
				{ // if there's something extra to add to the day
			 		content += "<br>";
			 		content += m_dayContent[monthDay-1];
				}
			}


			// place the table data tag
			cout << "<td width= \"" << m_width << "\" height=\"" << m_height << "\""
				<< " bgcolor=\"" << theColor << "\">" << endl;
			cout << content << endl;
			cout << "</td>" << endl;
		}
		// close the row
		cout << "</tr>" << endl;
	}
	// close the table
	cout << "</table>" << endl;
}
